package com.shrct.businessprocess.product.impl;

import com.shrct.businessprocess.organization.RCEnterprise;
import com.shrct.businessprocess.product.RCProductPurchaseInfo;
import com.shrct.businessprocess.product.RCProductPurchaseInfoDeliverTarget;
import com.shrct.businessprocess.product.impl.RCProductPurchaseInfoDeliverTargetImpl;
import com.shrct.businessprocess.product.proxy.RCProductPurchaseInfoProxy;
import com.shrct.businessprocess.project.RCProjectMoney;
import com.shrct.businessprocess.purchase.RCPurchaseOrderItem;
import com.shrct.businessprocess.purchase.impl.RCPurchaseOrderItemImpl;
import edu.fudan.mylang.pf.BaseModelObject;
import java.util.Collection;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.hibernate.annotations.DiscriminatorOptions;

@Entity
@DiscriminatorValue("RCPRODUCTPURCHASEINFO_TYPE")
@DiscriminatorOptions(force = true)
@DiscriminatorColumn(length = 64)
@Table(name = "rcproductpurchaseinfo")
public abstract class RCProductPurchaseInfoImpl extends BaseModelObject implements RCProductPurchaseInfo {
  public RCProductPurchaseInfoImpl() {
    super();
  }
  
  @JoinColumn(name = "orderItem_id")
  @ManyToOne(targetEntity = RCPurchaseOrderItemImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCPurchaseOrderItem orderItem;
  
  public RCPurchaseOrderItem getOrderItem() {
    return this.orderItem;
  }
  
  public RCProductPurchaseInfo setOrderItem(final RCPurchaseOrderItem orderItem) {
    this.orderItem = orderItem;
    return this;			
    
  }
  
  private Date inStockDate;
  
  public Date getInStockDate() {
    return this.inStockDate;
  }
  
  public RCProductPurchaseInfo setInStockDate(final Date inStockDate) {
    this.inStockDate = inStockDate;
    return this;			
    
  }
  
  private Integer inStockQuantity = Integer.valueOf(0);
  
  public Integer getInStockQuantity() {
    return this.inStockQuantity;
  }
  
  public RCProductPurchaseInfo setInStockQuantity(final Integer inStockQuantity) {
    this.inStockQuantity = inStockQuantity;
    return this;			
    
  }
  
  private Date deliverDate;
  
  public Date getDeliverDate() {
    return this.deliverDate;
  }
  
  public RCProductPurchaseInfo setDeliverDate(final Date deliverDate) {
    this.deliverDate = deliverDate;
    return this;			
    
  }
  
  private Integer deliverQuantity = Integer.valueOf(0);
  
  public Integer getDeliverQuantity() {
    return this.deliverQuantity;
  }
  
  public RCProductPurchaseInfo setDeliverQuantity(final Integer deliverQuantity) {
    this.deliverQuantity = deliverQuantity;
    return this;			
    
  }
  
  @OneToMany(targetEntity = RCProductPurchaseInfoDeliverTargetImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, mappedBy = "purchaseInfo")
  private Collection<RCProductPurchaseInfoDeliverTarget> deliverTargets = new java.util.ArrayList<com.shrct.businessprocess.product.RCProductPurchaseInfoDeliverTarget>();;
  
  public Collection<RCProductPurchaseInfoDeliverTarget> getDeliverTargets() {
    return this.deliverTargets;
  }
  
  public RCProductPurchaseInfoDeliverTarget createAndAddtoDeliverTargets() {
    getDeliverTargets().size();
    com.shrct.businessprocess.product.RCProductPurchaseInfoDeliverTarget rCProductPurchaseInfoDeliverTarget = new com.shrct.businessprocess.product.impl.RCProductPurchaseInfoDeliverTargetImpl(this);
    getObjectFactory().create(rCProductPurchaseInfoDeliverTarget);
    getDeliverTargets().add(rCProductPurchaseInfoDeliverTarget);
    return rCProductPurchaseInfoDeliverTarget;
    
  }
  
  public RCProductPurchaseInfoDeliverTarget createAndAddtoDeliverTargets(final Procedure1<RCProductPurchaseInfoDeliverTarget> updater) {
    getDeliverTargets().size();
    com.shrct.businessprocess.product.RCProductPurchaseInfoDeliverTarget rCProductPurchaseInfoDeliverTarget = new com.shrct.businessprocess.product.impl.RCProductPurchaseInfoDeliverTargetImpl(this);
    getObjectFactory().create(rCProductPurchaseInfoDeliverTarget);
    getDeliverTargets().add(rCProductPurchaseInfoDeliverTarget);
    return rCProductPurchaseInfoDeliverTarget;
    
  }
  
  public RCProductPurchaseInfo removeAllDeliverTargets() {
    for(RCProductPurchaseInfoDeliverTarget obj : this.deliverTargets){
    	getObjectFactory().delete(obj);
    }
    this.deliverTargets.clear();
    return this;	
    
  }
  
  public RCProductPurchaseInfo removeFromDeliverTargets(final RCProductPurchaseInfoDeliverTarget rCProductPurchaseInfoDeliverTarget) {
    this.deliverTargets.remove(rCProductPurchaseInfoDeliverTarget);
    getObjectFactory().delete(rCProductPurchaseInfoDeliverTarget);
    return this;
    
  }
  
  public void addDeliverTarget(final Integer quantity, final RCEnterprise deliverTarget) {
    RCProductPurchaseInfoDeliverTarget _createAndAddtoDeliverTargets = this.createAndAddtoDeliverTargets();
    final Procedure1<RCProductPurchaseInfoDeliverTarget> _function = new Procedure1<RCProductPurchaseInfoDeliverTarget>() {
        public void apply(final RCProductPurchaseInfoDeliverTarget it) {
          it.setQuantity(quantity);
          it.setDeliverTarget(deliverTarget);
        }
      };
    ObjectExtensions.<RCProductPurchaseInfoDeliverTarget>operator_doubleArrow(_createAndAddtoDeliverTargets, _function);
  }
  
  public RCProjectMoney getDeliveredAmount() {
    RCProjectMoney _unitPrice = this.orderItem.getUnitPrice();
    double _doubleValue = this.deliverQuantity.doubleValue();
    RCProjectMoney _multi = _unitPrice.multi(Double.valueOf(_doubleValue));
    return _multi;
  }
  
  public RCProductPurchaseInfoProxy toProxy() {
    com.shrct.businessprocess.product.proxy.RCProductPurchaseInfoProxy proxy = new com.shrct.businessprocess.product.proxy.RCProductPurchaseInfoProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
